package org.easy.tool.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import org.easy.tool.common.TimeUtil;
import org.easy.tool.entity.Problem;
import org.easy.tool.entity.Type;
import org.easy.tool.entity.TypeProblem;
import org.easy.tool.mapper.TypeMapper;
import org.easy.tool.mapper.TypeProblemMapper;
import org.easy.tool.service.IProblem;
import org.easy.tool.service.IType;
import org.easy.tool.service.ITypeProblem;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;
import java.util.stream.Collectors;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author Valley
 * @since 2023-08-25 14:20:43
 */
@Service
public class TypeProblemService extends ServiceImpl<TypeProblemMapper, TypeProblem> implements ITypeProblem {

    @Autowired
    TypeProblemMapper typeProblemMapper;

    @Autowired
    IType typeService;
    @Autowired
    IProblem problemService;

    @Override
    public List<Problem> getProblemListByType(String typeId) {
        LambdaQueryWrapper<TypeProblem> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TypeProblem::getTypeId, typeId);
        return typeProblemMapper.selectList(wrapper).stream().map(TypeProblem::getProblemId)
                .map(problemService::getProblemById).collect(Collectors.toList());
    }

    @Override
    public String addTypeProblem(String typeId, String problemId) {
        TypeProblem typeProblem = new TypeProblem();
        typeProblem.setTypeId(typeId);
        typeProblem.setProblemId(problemId);
        typeProblem.setCreateTime(TimeUtil.now());
        save(typeProblem);
        return typeProblem.getId();
    }
}
